# from zhipuai import ZhipuAI
# # string = sys.argv[1]
# client = ZhipuAI(api_key="3aa76c2b5fd0d22c19d644a75b3ffdd9.kNzUPyHwF88EUWhk") # 请填写您自己的APIKey
# response = client.chat.completions.create(
#     model="glm-4",  # 填写需要调用的模型名称
#     do_sample=True,
#     messages=[
#         {"role": "system", "content": "你是一个乐于解答各种问题的助手，你的任务是为用户提供专业、准确、有见地的建议。"},
#         # {"role": "user", "content": string},
#         {"role": "user", "content": "你好"},
#     ],
#     stream=True,
# )
# for chunk in response:
#     print(chunk.choices[0].delta.content)



import time
from zhipuai import ZhipuAI
import sys
client = ZhipuAI(api_key="3aa76c2b5fd0d22c19d644a75b3ffdd9.kNzUPyHwF88EUWhk") # 请填写您自己的APIKey
quesion = sys.argv[1]
# id = sys.argv[2]
# 用户询问的信息
messages=[
        {
            "role": "system",
            "content": "你是一个写代码的程序员,现在请用计算机专业术语对用户代码提供中文的优化建议"
        },
        {
            "role": "user",
            # "content": quesion
            "content": quesion
        }
    ]



response = client.chat.asyncCompletions.create(
    model="glm-4",  # 填写需要调用的模型名称
    messages=messages,
)

#  获取异步任务的结果
task_id = response.id
task_status = ''
get_cnt = 0

while task_status != 'SUCCESS' and task_status != 'FAILED' and get_cnt <= 40:
    result_response = client.chat.asyncCompletions.retrieve_completion_result(id=task_id)
    task_status = result_response.task_status
    time.sleep(2)
    get_cnt += 1
    if(task_status == 'SUCCESS'):
        choices=result_response.choices
        print(choices[0].message.content)
